package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.management.controller.form.SaveMenusForm;
import com.management.controller.info.MenusInfo;
import com.management.dao.RoleMenuDao;
import com.management.model.Menus;
import com.management.dao.MenusDao;
import com.management.model.RoleMenu;
import com.management.service.MenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.service.RoleMenuService;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusDao, Menus> implements MenusService {
   @Resource
    private MenusDao menusDao;
   @Resource
   private RoleMenuDao roleMenuDao;
   @Resource
   private RoleMenuService roleMenuService;
    @Override
    public Result findMenusByAccountId(Integer accountId) {
       List<Menus> list= menusDao.selectMenusByAccountId(accountId);
        List<MenusInfo> mis = CopyUtils.CopyList(list, MenusInfo.class);
        List<MenusInfo> result=new ArrayList<>();
        HashMap<Integer,MenusInfo> him=new HashMap<>();
        for (MenusInfo mi : mis) {
            if(mi.getMenuParent()==0){
                result.add(mi);
            }
            him.put(mi.getMenuId(),mi);
        }
        for (MenusInfo mi : mis) {
            MenusInfo parent = him.get(mi.getMenuParent());
            if(parent!=null){
                if(parent.getChildren()==null){
                    parent.setChildren(new ArrayList<>());

                }
                parent.getChildren().add(mi);
            }
        }

        return Result.getInstanceList(MenusInfo.class).setData(result);
    }

    @Override
    public Result getMenusByRoleNum(Integer roleNum) {
        List<Menus> list=baseMapper.getMenusByRoleNum(roleNum);
        List<Integer> list1=new ArrayList<>();
        for (Menus menus : list) {
            list1.add(menus.getMenuId());
        }
        return Result.getInstanceList(Integer.class).setData(list1);
    }

    @Override
    public Result getAllMenus() {
        List<Menus> list = list();
        List<MenusInfo> list1 = CopyUtils.CopyList(list, MenusInfo.class);
        List<MenusInfo> result=new ArrayList<>();
        HashMap<Integer,MenusInfo> him=new HashMap<>();
        for (MenusInfo mi : list1) {
            if(mi.getMenuParent()==0){
                result.add(mi);
            }
            him.put(mi.getMenuId(),mi);
        }
        for (MenusInfo mi : list1) {
            MenusInfo parent = him.get(mi.getMenuParent());
            if(parent!=null){
                if(parent.getChildren()==null){
                    parent.setChildren(new ArrayList<>());

                }
                parent.getChildren().add(mi);
            }
        }
        return Result.getInstanceList(MenusInfo.class).setData(result);
    }

    @Override
    public Result saveMenus(SaveMenusForm saveMenusForm) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",saveMenusForm.getRoleNum());
        roleMenuService.remove(queryWrapper);
        List<RoleMenu> list=new ArrayList<>();
        for (Integer integer : saveMenusForm.getMenusId()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(saveMenusForm.getRoleNum());
            roleMenu.setMenuId(integer);
            list.add(roleMenu);
        }
        roleMenuService.saveBatch(list);
        return Result.getInstance().setCode(200).setMessage("授权成功");
    }
}
